//
//  UserSettingViewController.m
//  LoveHealth
//
//  Created by lanou3g on 14-9-23.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "UserSettingViewController.h"
#import "UserSettingView.h"
#import "AboutViewController.h"
#import "UIImageView+WebCache.h"
#import "UserSettingTableViewCell.h"
#import "YDViewController.h"
//#import "SDImageCacheDelegate.h"
#import "SDImageCache.h"

#import "NetHelper.h"
#import "PrivacyViewController.h"

@interface UserSettingViewController ()
@property (nonatomic,retain)UserSettingView * settingView;
@property (nonatomic,retain)YDViewController * YDVC;
@end

@implementation UserSettingViewController
- (void)dealloc
{
    self.YDVC = nil;
    self.userInfoVC = nil;
    self.settingView = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.settingView = [[[UserSettingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease] ;
    [self.view addSubview:_settingView];
    
//    //导航栏返回按钮
//    UIBarButtonItem * leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(didClickReturnBackAction:)];
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
//    [leftBarButtonItem release];
    
    //设置代理
    _settingView.tableView.delegate = self;
    _settingView.tableView.dataSource = self;
}
////点击返回按钮触发事件
//- (void)didClickReturnBackAction:(UIBarButtonItem *)barButtonItem
//{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    
//}

#pragma mark-------------------------tableView------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_settingView.allArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //获取每个分区的cell数
    NSArray * oneGroup = _settingView.allArray[section];
    return [oneGroup count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UserSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[[UserSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]autorelease];
    }
    //cell是清理缓存时
    if (indexPath.section == 0 && indexPath.row == 1) {
      /*  //清理缓存显示cell  计算data文件夹大小
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        //找到data存放的路径
        NSString * filePath = [path stringByAppendingPathComponent:@"data"];
        //遍历data文件夹得到文件夹大小
        NSArray * array = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
        long size = 0;
        for (int i = 0; i < [array count]; i ++) {
            NSString * fullPath = [filePath stringByAppendingPathComponent:[array objectAtIndex:i]];
            NSDictionary * fileAttributeDic = [fileManager attributesOfItemAtPath:fullPath error:nil];
            //size字节
            size+= fileAttributeDic.fileSize;
        }
        */
        NSArray * oneGroup = _settingView.allArray[indexPath.section];
        NSString * str = oneGroup[indexPath.row];
        //图片缓存加上data文件夹大小
        
        CGFloat totlefileSize = (CGFloat)([[SDImageCache sharedImageCache]getSize] +[[NetHelper shareInstance]cachesDataSize])/1024.0/1024.0;
        
        //float totlefileSize = [[NSNumber numberWithUnsignedInteger:([[SDImageCache sharedImageCache] getSize] + size)/1024] floatValue]/1024.0;
        cell.tag = 120;
        cell.label.text = [str stringByAppendingFormat:@"                         %.1fM",totlefileSize];
        NSArray * oneArray = _settingView.picArray[indexPath.section];
        cell.iconView.image = oneArray[indexPath.row];

        
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        NSArray * oneGroup = _settingView.allArray[indexPath.section];
        NSString * str = oneGroup[indexPath.row];
        cell.label.text = str;
        NSArray * oneArray = _settingView.picArray[indexPath.section];
        cell.iconView.image = oneArray[indexPath.row];

    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * oneGroup = _settingView.allArray[indexPath.section];
    NSString * string = oneGroup[indexPath.row];
    
    if (YES == [string isEqualToString:@"隐私声明"]) {
        PrivacyViewController * privacyVC = [[PrivacyViewController alloc]init];
        privacyVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentViewController:privacyVC animated:YES completion:nil];
        [privacyVC release];
    }
    if (YES == [string isEqualToString:@"关于爱健康"]) {
        AboutViewController * aboutVC = [[AboutViewController alloc] init];
        aboutVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentViewController:aboutVC animated:YES completion:nil];
        [aboutVC release];
    }
    if (YES == [string isEqualToString:@"清理缓存"]) {

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"确定清理？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 105;
        [alert show];
        [alert release];
//        //删除cache下的data文件夹
//        NSFileManager * fileManager = [NSFileManager defaultManager];
//        NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//        //找到data存放的路径
//        NSString * filePath = [path stringByAppendingPathComponent:@"data"];
//        //删除data文件夹
//        [fileManager removeItemAtPath:filePath error:nil];
//        //cell显示清除后的缓存
//        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//        cell.textLabel.text = [string stringByAppendingFormat:@"                               %.1fM",0.0];
//        //清理图片缓存
//        [[SDImageCache sharedImageCache] clearDisk];
//        //清理成功提示
//
//        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"清除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
//        [alert show];
//        [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1];
    }
 
    if (YES == [string isEqualToString:@"检测更新"]) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"当前版本为最新版本" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    if (YES == [string isEqualToString:@"注销登录"]) {
        
        if ([[NSUserDefaults standardUserDefaults] objectForKey:KLoginUserInfo] == nil) {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还没有登陆哦" delegate:nil    cancelButtonTitle:nil otherButtonTitles:nil];
            [alert show];
            
            [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1];
        }else{
            
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否注销" delegate:self    cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alert.tag = 110;
            [alert show];
            [alert release];
        }
        
        
        
        
    }
    if (YES == [string isEqualToString:@"帮助"]) {
        _YDVC = [[YDViewController alloc]init];
        _YDVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:self.YDVC animated:YES completion:^{
            [_YDVC.inButton addTarget:self action:@selector(didClickYDVCbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        }];
     
        
    }
}
- (void)didClickYDVCbuttonAction:(UIButton *)btn
{
    [self.YDVC dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark------------------------------UIAlertViewDelegate------------------------
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 105) {
        if (buttonIndex == 1) {
            /*
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        //找到data存放的路径
        NSString * filePath = [path stringByAppendingPathComponent:@"data"];
        //删除data文件夹
        [fileManager removeItemAtPath:filePath error:nil];*/
            
            [[NetHelper shareInstance]cleanCachesData];
            
        //cell显示清除后的缓存
//            NSArray * oneGroup = _settingView.allArray[0];
//            NSString * string = oneGroup[1];
////        UITableViewCell * cell = [[self.settingView.tableView cellForRowAtIndexPath:in] viewWithTag:120];
////        cell.textLabel.text = [string stringByAppendingFormat:@"                               %.1fM",0.0];
        //清理图片缓存
        [[SDImageCache sharedImageCache] clearDisk];
       //清理成功提示
        
        
            [self.settingView.tableView reloadData];
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"清除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
        [alert show];
            
       [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1];
        }else{
            
        }
    }else if(alertView.tag == 110){
        if (buttonIndex == 0) {
            
        }else{
            
            
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults removeObjectForKey:KLoginUserInfo];
                //同步数据
                [userDefaults synchronize];
                [self.userInfoVC.tableView reloadData];
                
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"已注销" message:nil delegate:nil    cancelButtonTitle:nil otherButtonTitles:nil];
                [alert show];
                
                [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1];

            
          
        }
    }
}


- (void)dismissAlert:(UIAlertView *)alert
{
    if (alert) {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [alert release];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
